package com.situ.mapper;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.situ.domain.User;
import java.util.List; 


public class UserMapperImpl implements UserMapper {
	
		// 1.输入流 获取mybatis核心文件
		InputStream inputStream = 
				this.getClass().getResourceAsStream("/mybatis.xml");
		
		// 2.启动工厂
		SqlSessionFactory factory =
				new SqlSessionFactoryBuilder().build(inputStream);
		
		// 3.启动Session
		SqlSession session = factory.openSession();
		
	@Override
	public void insert(User user) {
		

		
		// 4.执行语句
		session.insert("UserMapper.insert", user);
		
		// 5.提交
		session.commit();
		
		// 6.关闭 
		session.close();
		
		System.out.println("添加完成");
		
		
	}

	@Override
	public void deleteById(Integer id) {
		
		// 4.执行语句
		session.insert("UserMapper.deleteById", id);
		
		// 5.提交
		session.commit();
		
		// 6.关闭 
		session.close();
		
		System.out.println("删除完成");
	}

	
	/**
	 *@return Integer 是否成功 1成功，-1失败
	 */
	@Override
	public void updateById(Integer id) {
		// TODO Auto-generated method stub
	}

	
	
	@Override
	public void selectById(Integer id) {
		
		// 4.执行语句
		User user = (User) session.selectOne("UserMapper.selectById",id);
		
		System.out.println(user);
		
		// 6.关闭 
		session.close();
	}

	@Override
	public List<User> selectAll() {
		
		
		// 4.执行语句
		List<User> userList = 
				session.selectList("UserMapper.selectAll");
		
		session.commit();
		
		// 6.关闭 
		session.close();
		
		return userList;
		
	}

}
